package cn.gemiman.daoyi.module.iot.controller.admin.kafkamsglogs;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;
import cn.gemiman.daoyi.module.iot.controller.admin.kafkamsglogs.vo.KafkaMsgLogsPageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.kafkamsglogs.vo.KafkaMsgLogsRespVO;
import cn.gemiman.daoyi.module.iot.controller.admin.kafkamsglogs.vo.KafkaMsgLogsSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.kafkamsglogs.KafkaMsgLogsDO;
import cn.gemiman.daoyi.module.iot.service.kafkamsglogs.KafkaMsgLogsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - kafka原始消息")
@RestController
@RequestMapping("/iot/kafka-msg-logs")
@Validated
public class KafkaMsgLogsController {

    @Resource
    private KafkaMsgLogsService kafkaMsgLogsService;

    @PostMapping("/create")
    @Operation(summary = "创建kafka原始消息")
    @PreAuthorize("@ss.hasPermission('iot:kafka-msg-logs:create')")
    public CommonResult<Long> createKafkaMsgLogs(@Valid @RequestBody KafkaMsgLogsSaveReqVO createReqVO) {
        return success(kafkaMsgLogsService.createKafkaMsgLogs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新kafka原始消息")
    @PreAuthorize("@ss.hasPermission('iot:kafka-msg-logs:update')")
    public CommonResult<Boolean> updateKafkaMsgLogs(@Valid @RequestBody KafkaMsgLogsSaveReqVO updateReqVO) {
        kafkaMsgLogsService.updateKafkaMsgLogs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除kafka原始消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:kafka-msg-logs:delete')")
    public CommonResult<Boolean> deleteKafkaMsgLogs(@RequestParam("id") Long id) {
        kafkaMsgLogsService.deleteKafkaMsgLogs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得kafka原始消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:kafka-msg-logs:query')")
    public CommonResult<KafkaMsgLogsRespVO> getKafkaMsgLogs(@RequestParam("id") Long id) {
        KafkaMsgLogsDO kafkaMsgLogs = kafkaMsgLogsService.getKafkaMsgLogs(id);
        return success(BeanUtils.toBean(kafkaMsgLogs, KafkaMsgLogsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得kafka原始消息分页")
    @PreAuthorize("@ss.hasPermission('iot:kafka-msg-logs:query')")
    public CommonResult<PageResult<KafkaMsgLogsRespVO>> getKafkaMsgLogsPage(@Valid KafkaMsgLogsPageReqVO pageReqVO) {
        PageResult<KafkaMsgLogsDO> pageResult = kafkaMsgLogsService.getKafkaMsgLogsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KafkaMsgLogsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出kafka原始消息 Excel")
    @PreAuthorize("@ss.hasPermission('iot:kafka-msg-logs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKafkaMsgLogsExcel(@Valid KafkaMsgLogsPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KafkaMsgLogsDO> list = kafkaMsgLogsService.getKafkaMsgLogsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "kafka原始消息.xls", "数据", KafkaMsgLogsRespVO.class,
                BeanUtils.toBean(list, KafkaMsgLogsRespVO.class));
    }

}
